package com.young.springdemo.config.exception;

import com.young.springdemo.config.red.RedTeacherException;
import com.young.springdemo.service.annotation.ParamBlankException;
import com.young.springdemo.utils.domain.CodeMsg;
import com.young.springdemo.utils.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: springdemo
 * @description 自定义异常处理
 * @author: Young
 * @create: 2020-05-29 15:06
 **/
@RestControllerAdvice
@Slf4j
public class GlobeException extends RuntimeException {

    @ExceptionHandler({ParamBlankException.class})
    public Result requestMissingServletRequest(Exception ex){
        log.error("参数为空参数处理,异常[{}]",ex.getMessage(),ex);
        return Result.error(CodeMsg.PARAM_EXCEPTION);
    }
    @ExceptionHandler(value = Exception.class)
    public Result errorHandler(HttpServletRequest request, Exception e){
        log.error("全局异常处理,异常[{}]",e.getMessage(),e);
        return Result.error(CodeMsg.SERVER_EXCEPTION);
    }
    @ExceptionHandler({RedTeacherException.class})
    public Result redValidate(Exception ex) {
        log.error("红名单参数处理,异常[{}]", ex.getMessage(),ex);
        return Result.error(CodeMsg.RED_PERSON);
    }
}
